#pragma once
#include "calserver.hpp"
#include <memory>
using namespace sxj;
using namespace std;

enum{
    ok = 0,
    DIV_ZERO,
    MOD_ZERO,
    OP_ERROR,
}

static void  Usage(string proc)
{
    cout<<"\nUsage:\n\t"<<proc<<"local_port\n\n";
}

//req:里面一定是我们处理好的一个完整的请求对象
//resp:根基req，进行业务处理，填充resp，不用管理任何读取和写入，序列化反序列化等结果
bool cal(const Request& req,Response &resp)
{
    resp.exitcode = ok;
    resp.result = ok;
    
    switch(req.op)
    {
        case '+':
        resp.result = req.x + req.y;
        break;
        case '-':
        resp.result = req.x - req.y;
        break;
        case '*':
        resp.result = req.x * req.y;
        break;
        case '/':
        {
            if(req.y == 0) 
                resp.exitcode = DIV_ZERO;
            else 
                resp.result = req.x / req.y;
        }
        break;
        case '%':
        {
            if(req.y == 0) 
                resp.exitcode = MOD_ZERO;
            else 
                resp.result = req.x % req.y;
        }
        break;
        default:
        resp.exitcode = OP_ERROR;
        break;
    }
    return true;
}


int main(int argc,char *argv[])
{
    if(argc != 2)
    {
        Usage(argv[0]);
        exit(-1);
    }
    uint16_t port = atoi(argv[1]);

    unique_ptr<tcpserver> tsvr(new tcpserver(port));
    tsvr->initServer();
    tsvr->start(cal);
}